﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ZPLPrintDLL;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.Net;
using System.Drawing;

namespace ZPLPrint
{
    public partial class Frm_LWYL : Form
    {
        PrintHelper PrintH = new PrintHelper();
        CommonHelper CommonH = new CommonHelper();
        private RawPrinterHelper zplp = new RawPrinterHelper();
        private string FilePath = string.Empty;
        public Frm_LWYL()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            setMb();
            GetPrintList();
        }
        /// <summary>
        /// 设置标签模板
        /// </summary>
        private void setMb()
        {
            FilePath = Application.StartupPath + @"\template";

            if (Directory.Exists(FilePath) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(FilePath);
            }
            //遍历路径设置列表
            try
            {
                List<string> list = CommonH.GetFileNameListByPath(FilePath, ".prn");
                comboBox1.DataSource = list;
            }
            catch (Exception)
            {
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string path = FilePath + comboBox1.Text;
            string PrintStr = textBox2.Text;

            string Print = PrintH.GetPrintString(path, PrintStr);//替换后字符串
            textBox1.Text = Print;

            bool tmpPD = zplp.SendStringToPrinter(comboBox2.Text, Print);
            if (tmpPD == false)
            {
                MessageBox.Show("打印错误，检查后请重新操作！");
            }
        }
        private void GetPrintList()
        {
            List<string> lt = new List<string>();
            var printers = PrinterSettings.InstalledPrinters;
            foreach (var item in printers)
            {
                lt.Add(item.ToString());
            }
            comboBox2.DataSource = lt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == "")
            {
                MessageBox.Show("请先选择模板！");
            }

            string path = FilePath + comboBox1.Text;
            string Print = CommonH.getBoxFlie(path);
            textBox3.Text = Print;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("explorer", linkLabel1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("模板内容为空，请稍后重新操作！");
            }
            string ImageFilePath = Application.StartupPath + @"\image";

            if (Directory.Exists(ImageFilePath) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(ImageFilePath);
            }

            byte[] zpl = Encoding.UTF8.GetBytes(textBox3.Text);

            // adjust print density (8dpmm), label width (4 inches), label height (6 inches), and label index (0) as necessary
            var request = (HttpWebRequest)WebRequest.Create("http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/");
            request.Method = "POST";
            //request.Accept = "application/pdf"; // omit this line to get PNG images back
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = zpl.Length;

            var requestStream = request.GetRequestStream();
            requestStream.Write(zpl, 0, zpl.Length);
            requestStream.Close();

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                var responseStream = response.GetResponseStream();
                var fileStream = File.Create(ImageFilePath + @"\label1.PNG"); // change file name for PNG images
                responseStream.CopyTo(fileStream);
                responseStream.Close();
                fileStream.Close();
                FileStream fileStreamImg = new FileStream(ImageFilePath + @"\label1.PNG", FileMode.Open, FileAccess.Read);
                pictureBox1.Image = Image.FromStream(fileStreamImg);
                fileStreamImg.Close();
                fileStreamImg.Dispose();
            }
            catch (Exception ex)
            {

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("模板内容为空，请稍后重新操作！");
            }
            string ImageFilePath = Application.StartupPath + @"\image";

            if (Directory.Exists(ImageFilePath) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(ImageFilePath);
            }

            byte[] zpl = Encoding.UTF8.GetBytes(textBox1.Text);

            // adjust print density (8dpmm), label width (4 inches), label height (6 inches), and label index (0) as necessary
            var request = (HttpWebRequest)WebRequest.Create("http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/");
            request.Method = "POST";
            //request.Accept = "application/pdf"; // omit this line to get PNG images back
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = zpl.Length;

            var requestStream = request.GetRequestStream();
            requestStream.Write(zpl, 0, zpl.Length);
            requestStream.Close();

            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                var responseStream = response.GetResponseStream();
                var fileStream = File.Create(ImageFilePath + @"\label2.PNG"); // change file name for PNG images
                responseStream.CopyTo(fileStream);
                responseStream.Close();
                fileStream.Close();
                FileStream fileStreamImg = new FileStream(ImageFilePath + @"\label2.PNG", FileMode.Open, FileAccess.Read);
                pictureBox2.Image = Image.FromStream(fileStreamImg);
                fileStreamImg.Close();
                fileStreamImg.Dispose();
            }
            catch (Exception ex)
            {

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "")
            {
                MessageBox.Show("请先加载模板！");
            }

            string path = FilePath + comboBox1.Text;
            string PrintStr = textBox2.Text;
            string Print = PrintH.GetPrintString(path, PrintStr);
            textBox1.Text = Print;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string path = FilePath + comboBox1.Text;
            string PrintStr = textBox2.Text;//模板

            string Print1 = PrintH.GetPrintString(path, PrintStr);//替换后字符串
            textBox1.Text = Print1;

            StringBuilder szStr = new StringBuilder();
            try
            {
                int PrintCount = Convert.ToUInt16(textBox4.Text);
                for (int i = 0; i < PrintCount; i++)
                {
                    string Print = PrintH.GetPrintString(path, PrintStr);//替换后字符串
                    if (i != 0)
                    {
                        Print = Print.Replace("^XA~TA000", "~TA000");//去掉开始命令^XA
                    }
                    Print = Print.Replace("锘?", "");//去除有影响的特殊字符
                    szStr.Append(Print);
                }
            }
            catch (Exception)
            {
            }

            bool tmpPD = zplp.SendStringToPrinter(comboBox2.Text, szStr.ToString());
            if (tmpPD == false)
            {
                MessageBox.Show("打印错误，检查后请重新操作！");
            }

        }
    }
}
